package com.project.weimi.fragment;

import java.util.ArrayList;
import java.util.List;

import com.project.weimi.R;
import com.project.weimi.adapter.CollectionInvitationAdapter;
import com.project.weimi.entity.response.CollectionInvitationEntity;
import com.project.weimi.utils.SetClickListener;
import com.project.weimi.widget.ToastMessage;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class InvitationFragment extends BaseFragment implements OnRefreshListener{

	private RelativeLayout rl_none;
	private Button btn_go,btn_delete;
	private TextView tv_type;
	private LinearLayout ll_recyclerView,ll_checkall;
	private CheckBox checkbox_all;
	
	private SwipeRefreshLayout swipeRefresh;
	private RecyclerView recyclerView;
	private LinearLayoutManager layoutManager;
	private CollectionInvitationAdapter adapter;
	private int lastVisibleItem;
	
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			
			switch (msg.what) {
			case 0:
				swipeRefresh.setRefreshing(false);
				adapter.getList().clear();
				addList();
				ToastMessage.showMyToast(getActivity(), "刷新完成");
				break;
			case 1:
				addList();
				break;
			default:
				break;
			}
		};
	};
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// 解析布局文件
		View view = inflater.inflate(R.layout.fragment_mycollection_invitation, null);
		
		initView(view);
		
		
		return view;
	}
	
	@SuppressWarnings("deprecation")
	private void initView(View view){
		rl_none = (RelativeLayout)view.findViewById(R.id.rl_none);
		btn_go = (Button)view.findViewById(R.id.btn_go);
		btn_delete = (Button)view.findViewById(R.id.btn_delete);
		tv_type = (TextView)view.findViewById(R.id.tv_type);
		ll_recyclerView = (LinearLayout)view.findViewById(R.id.ll_recyclerView);
		ll_checkall = (LinearLayout)view.findViewById(R.id.ll_checkall);
		checkbox_all = (CheckBox)view.findViewById(R.id.checkbox_all);
		
		swipeRefresh = (SwipeRefreshLayout)view.findViewById(R.id.swipeRefresh);
		recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
		
		swipeRefresh.setColorScheme(R.color.color1,R.color.color2,R.color.color3,R.color.color4);
		swipeRefresh.setOnRefreshListener(this);
		
		recyclerView.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
				super.onScrollStateChanged(recyclerView, newState);
				
				if(adapter.getItemCount() < 5){
					return;
				}
				
				if(newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()){
					handler.sendEmptyMessageDelayed(1, 3000);
				}
			}
			
			@Override
			public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
				super.onScrolled(recyclerView, dx, dy);
				lastVisibleItem = layoutManager.findLastVisibleItemPosition();
			}
			
		});
		
		SetClickListener.setClickListener(onClickListener, btn_go,btn_delete,checkbox_all);
		
		recyclerView.setHasFixedSize(true);
		layoutManager = new LinearLayoutManager(getActivity());
		recyclerView.setLayoutManager(layoutManager);
		recyclerView.setItemAnimator(new DefaultItemAnimator());
		
		adapter = new CollectionInvitationAdapter(getActivity());
		recyclerView.setAdapter(adapter);
		
		if(adapter.getList() == null || adapter.getList().size() <= 0){
			ll_recyclerView.setVisibility(View.GONE);
			rl_none.setVisibility(View.VISIBLE);
		}
		
	}
	
	private void addList(){
		ll_recyclerView.setVisibility(View.VISIBLE);
		rl_none.setVisibility(View.GONE);
		List<CollectionInvitationEntity> list = getList();
		adapter.getList().addAll(list);
		adapter.notifyDataSetChanged();
	}
	
	private List<CollectionInvitationEntity> getList(){
		List<CollectionInvitationEntity> list = new ArrayList<CollectionInvitationEntity>();
		int size = adapter.getList().size();
		for (int i = 0; i < 10; i++) {
			list.add(new CollectionInvitationEntity(false,"秀一波儿腿谢谢大家来看我直播" + (size + i), "美女直播间", "188"));
		}
		
		return list;
	}
	
	private OnClickListener onClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
			case R.id.btn_go:
				addList();
				break;
			case R.id.btn_delete: //删除按钮
				int falseCount = 0;
//				for (int i = 0; i < adapter.getList().size(); i++) {
//					boolean b = adapter.getList().get(i).isCheckbox();
//					if(b == false){
//						falseCount++;
//					}
//				}
//				if(falseCount == adapter.getList().size()){
//					ToastMessage.showMyToast(getActivity(), "请选择删除项");
//					return;
//				}
				
				
				List<Integer> deleteItem = new ArrayList<Integer>();
				for (int i = 0; i < adapter.getList().size(); i++) {
					boolean b = adapter.getList().get(i).isCheckbox();
					if(b == true){
						deleteItem.add(i);
					}
					else if(b == false){
						falseCount++;
					}
				}
				
				if(falseCount == adapter.getList().size()){
					ToastMessage.showMyToast(getActivity(), "请选择删除项");
					return;
				}
				
				for (int j = deleteItem.size(); j > 0; j--) {
					adapter.getList().remove(adapter.getList().get(deleteItem.get(j - 1)));
				}
				
				adapter.notifyDataSetChanged();
				isAllCheck();
				break;
//			case R.id.ll_checkall: //全选LinearLayout
//				
//				break;
			case R.id.checkbox_all: //全选CheckBox
				
				if(checkbox_all.isChecked()){
					for (int i = 0; i < adapter.getList().size(); i++) {
						adapter.getList().get(i).setCheckbox(true);
					}
				}else {
					for (int i = 0; i < adapter.getList().size(); i++) {
						adapter.getList().get(i).setCheckbox(false);
					}
				}
				adapter.notifyDataSetChanged();
				
				break;
			default:
				break;
			}
		}
	};
	
	public void isAllCheck(){
		if(null == adapter.getList() || adapter.getList().size() <= 0){
			checkbox_all.setChecked(false);
			ll_recyclerView.setVisibility(View.GONE);
			rl_none.setVisibility(View.VISIBLE);
			return;
		}
		
		int checkCount = 0;
		for (int i = 0; i < adapter.getList().size(); i++) {
			boolean b = adapter.getList().get(i).isCheckbox();
			if(b == true){
				checkCount++;
			}
		}
		
		if(checkCount > 0 && checkCount == adapter.getList().size()){
			checkbox_all.setChecked(true);
		}else if(checkCount > 0 && checkCount < adapter.getList().size() || checkCount == 0){
			checkbox_all.setChecked(false);
		}
	}
	
	
	@Override
	public void onRefresh() {
		handler.sendEmptyMessageDelayed(0, 3000);
	}
	
	
}
